#!/usr/bin/env bash
# vim: fdm=marker fmr=[[[,]]] cms=\ #%s

OS=$(uname | tr '[:upper:]' '[:lower:]')
CPU=$(uname -m | tr '[:upper:]' '[:lower:]')
VERSION=
SOAR_DIR=/opt/soar
RELEASE_DIR=
ALLPKG=
export CC=gcc
export MAKE
case $OS in
linux)
    MAKE=make
    STAT='stat -c %s'
    ;;
freebsd)
    MAKE=gmake
    STAT='stat -f %z'
    ;;
esac

build_common()
{ #[[[
    cd common/c         # /common/c
    $MAKE clean
    $MAKE release
    if (( $? != 0 ))
    then
        echo "Error: compile lib release fail"
        return 1
    fi
    $MAKE
    if (( $? != 0 ))
    then
        echo "Error: compile lib debug fail"
        return 1
    fi
    cd ../..            # /
    tar --exclude ".svn" -rf $ALLPKG \
        common/c/include \
        common/c/lib/{libsoar_g.a,libsoar.a} \
        common/c/sample

    tar --exclude ".svn" --exclude "*.pyc" -rf $ALLPKG common/python
    tar --exclude ".svn" -rf $ALLPKG common/bash
    tar --exclude ".svn" -rf $ALLPKG common/php

    tar --exclude ".svn" -rf $ALLPKG bin README VERSION

    echo "Build common succeed"
} #]]]

build_web()
{ #[[[
    tar --exclude ".svn" -rf $ALLPKG web

    echo "Build web succeed"
} #]]]

build_svcnode()
{ #[[[
    local sns=(sn_log sn_passwd sn_session sn_secure sn_mailfwd sn_confirm)

    cd svcnode          # /svcnode
    local sn
    for sn in ${sns[*]}
    do
        cd $sn
        $MAKE clean
        $MAKE release
        if (( $? != 0 ))
        then
            echo "Error: compile $sn fail"
            return 1
        fi
        echo "----------------------------------------"
        cd ..
    done
    cd ..               # /

    tar --exclude ".svn" -rf $ALLPKG svcnode/{README,bootall,downall}
    tar --exclude ".svn" -rf $ALLPKG svcnode/tool
    for sn in ${sns[*]}
    do
        tar --exclude ".svn" -rf $ALLPKG svcnode/$sn/{svrctl,server.cnfg,$sn,tool}
    done
    tar --exclude ".svn" -rf $ALLPKG svcnode/sn_passwd/db/initdb
    tar --exclude ".svn" -rf $ALLPKG svcnode/sn_secure/key
    tar --exclude ".svn" -rf $ALLPKG svcnode/sn_mailfwd/black.lst
    tar --exclude ".svn" -rf $ALLPKG svcnode/sn_confirm/font/DejaVuSansMono.ttf

    echo "Build svcnode succeed"
} #]]]

_lite()
{ #[[[#
    cd svcenv/lite      # /svcenv/lite

    cd sys              # /svcenv/lite/sys

    cd agent            # /svcenv/lite/sys/agent
    $MAKE clean
    $MAKE release
    if (( $? != 0 ))
    then
        echo "Error: compile svcenv.lite.sys.agent fail"
        return 1
    fi
    cd ..               # /svcenv/lite/sys

    cd skeleton         # /svcenv/lite/sys/skeleton
    $MAKE clean
    $MAKE
    if (( $? != 0 ))
    then
        echo "Error: compile svcenv.lite.sys.skeleton fail"
        return 1
    fi
    cd ..               # /svcenv/lite/sys

    cd ..               # /svcenv/lite

    cd ../..            # /

    tar --exclude ".svn" -rf $ALLPKG \
    svcenv/lite/etc/svcenv.sh svcenv/lite/etc/svcenv.cnfg svcenv/lite/src svcenv/lite/tool svcenv/lite/sys/agent/seagent svcenv/lite/sys/skeleton/svr_skeleton.h svcenv/lite/sys/skeleton/svr_skeleton.a
} #]]]#

_tuxedo()
{ #[[[#
    if [[ $OS != linux ]]
    then
        echo "Warning: OS not Linux, skip svcenv.tuxedo"
        return 0
    fi
    if [[ -z $TUXDIR || ! -x $TUXDIR/bin/buildclient ]]
    then
        echo "Error: Tuxedo not installed, skip svcenv.tuxedo"
        return 0
    fi

    cd svcenv/tuxedo    # /svcenv/tuxedo

    cd sys              # /svcenv/tuxedo/sys

    cd agent            # /svcenv/tuxedo/src/agent
    $MAKE clean
    $MAKE release
    if (( $? != 0 ))
    then
        echo "Error: compile svcenv.tuxedo.sys.agent fail"
        return 1
    fi
    cd ..               # /svcenv/tuxedo/sys

    cd ..               # /svcenv/tuxedo

    cd ../..            # /

    tar --exclude ".svn" -rf $ALLPKG \
    svcenv/tuxedo/etc/svcenv.sh svcenv/tuxedo/etc/svcenv.cnfg svcenv/tuxedo/src tuxedo/tool svcenv/tuxedo/sys/agent/seagent svcenv/tuxedo/sys/skeleton/svr_skeleton.h svcenv/tuxedo/sys/skeleton/svr_skeleton.c
} #]]]#

_php()
{ #[[[
    tar --exclude ".svn" -rf $ALLPKG \
    svcenv/php/etc/svcenv.sh svcenv/php/etc/svcenv.cnfg.inc svcenv/php/src svcenv/php/tool svcenv/php/sys/seagent.php
} #]]]

build_svcenv()
{ #[[[#
    # lite
    _lite || return 1
    echo "----------------------------------------"

    # tuxedo
    _tuxedo || return 1
    echo "----------------------------------------"

    # php
    _php || return 1
    echo "----------------------------------------"

    echo "Build svcenv succeed"
} #]]]#

create_bin()
{ #[[[#
    cd $RELEASE_DIR     # /build/_release

    local bin_block_size=1024
    local ins_size=$($STAT ../install_tmpl.sh)
    local allpkg_start=$((ins_size / bin_block_size))
    if ((ins_size%bin_block_size != 0))
    then ((++allpkg_start)); fi
    local allpkg=$(basename $ALLPKG).gz
    local allpkg_size=$($STAT $allpkg)
    local allpkg_block=$((allpkg_size/bin_block_size))
    if ((allpkg_size%bin_block_size != 0))
    then ((++allpkg_block)); fi
#    echo "pkg:$allpkg start:$allpkg_start size:$allpkg_size block:$allpkg_block" 
    local BIN=soar_${OS}_${CPU}_${VERSION}.bin
    rm -f $BIN
    awk '
{
    gsub("%%BIN_BLOCK_SIZE%%", "'$bin_block_size'");
    gsub("%%ALLPKG%%", "'$allpkg'");
    gsub("%%ALLPKG_START%%", "'$allpkg_start'");
    gsub("%%ALLPKG_SIZE%%", "'$allpkg_size'");
    print;
}
' ../install_tmpl.sh >$BIN
    dd if=$allpkg of=$BIN bs=$bin_block_size count=$allpkg_block seek=$allpkg_start >/dev/null 2>&1
    if (($? != 0))
    then
        echo "Error: Attach package($allpkg) fail"
        return 1
    fi
    chmod a+x $BIN

    cd ../..            # /

    echo "Create BIN succeed"
} #]]]#

main()
{ #[[[#
    local ROOT_DIR=$(dirname $0)
    if [[ ${ROOT_DIR:0:1} != '/' ]]
    then
        ROOT_DIR=$PWD/$ROOT_DIR
    fi
    cd $ROOT_DIR
    VERSION=$(cat VERSION)
    RELEASE_DIR=$ROOT_DIR/build/_release
    mkdir $RELEASE_DIR
    ALLPKG=$RELEASE_DIR/soar.tar
    rm -f $ALLPKG ${ALLPKG}.gz

    #--- common ---#
    build_common
    if (( $? != 0 ))
    then
        echo "Error: build common fail"
        return 1
    fi
    echo
    echo "=================================================="
    echo

    #--- web ---#
    build_web
    if (( $? != 0 ))
    then
        echo "Error: build web fail"
        return 1
    fi
    echo
    echo "=================================================="
    echo

    #--- svcnode ---#
    build_svcnode
    if (( $? != 0 ))
    then
        echo "Error: build svcnode fail"
        return 1
    fi
    echo
    echo "=================================================="
    echo

    #--- svcenv ---#
    build_svcenv
    if (( $? != 0 ))
    then
        echo "Error: build svcenv fail"
        return 1
    fi
    echo
    echo "=================================================="
    echo

    gzip $ALLPKG

    #--- generate install binary package ---#
    create_bin
    if (( $? != 0 ))
    then
        echo "Error: create install binary package fail"
        return 1
    fi
    echo
    echo "=================================================="
    echo

    echo "Building succeed"
    echo
    return 0
} #]]]#

main
exit $?
